<template>
  <div class="user_wrap">
    <h3>个人资料</h3>
    <div class="user_info">
      <div class="user_avatar">
        <img
          src="https://portrait.gitee.com/uploads/avatars/user/2184/6553316_lu-sir_1593787900.jpeg!avatar100"
          alt
        />
        <div class="updata_avatar"><a-icon type="plus-circle" />更换头像</div>
      </div>
      <div class="user_detail">
        <div class="detail_header">
          <div><a-icon type="usergroup-delete" />基本信息</div>
          <div @click="Edit()"><a-icon type="edit" />编辑</div>
        </div>
        <div class="detail_body" v-if="isEdit == false">
          <div class="detail_item">
            <span class="item_title">姓名</span>
            <span class="item_content">{{ teacherData.name }}</span>
          </div>
          <div class="detail_item">
            <span class="item_title">性别</span>
            <span class="item_content">
              {{ teacherData.sex == true ? "男" : "女" }}
            </span>
          </div>
          <div class="detail_item">
            <span class="item_title">所在院系</span>
            <span class="item_content">{{ teacherData.collegeName }}</span>
          </div>
          <div class="detail_item">
            <span class="item_title">联系电话</span>
            <span class="item_content">{{ teacherData.phoneNumber }}</span>
          </div>
          <div class="detail_item">
            <span class="item_title">电子邮箱</span>
            <span class="item_content">
              {{ teacherData.email }}
            </span>
          </div>
        </div>

        <div class="teacherInfo" v-if="isEdit">
          <a-form-model ref="teacherData" :model="teacherInput" :rules="rules">
            <a-form-model-item prop="phoneNumber">
              <span>联系电话：</span>
              <a-input
                class="phone"
                ref="phoneNumber"
                v-model="teacherInput.phoneNumber"
                placeholder="手机号码，13位"
                allow-clear
              ></a-input>
            </a-form-model-item>
            <a-form-model-item prop="email">
              <span>电子邮箱：</span>
              <a-input
                class="phone"
                ref="email"
                v-model="teacherInput.email"
                placeholder="电子邮箱"
                allow-clear
              ></a-input>
              <a-button
                class="putTeacherInfo"
                type="primary"
                size="large"
                @click="UpdateTeacherInfo()"
                >提交</a-button
              >
            </a-form-model-item>
          </a-form-model>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
const checkPhone = (rule, value, callback) => {
  if (value) {
    if (!/^1(3|4|5|6|7|8|9)d{9}$/.test(value)) {
      callback(new Error("请输入正确的手机号码"));
    }
  }
  callback();
};
const checkEmail = (rule, value, callback) => {
  if (value) {
    if (
      !/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(
        value
      )
    ) {
      callback(new Error("请输入正确的邮箱"));
    }
  }
  callback();
};

export default {
  data() {
    return {
      isEdit: false,
      teacherInput: {
        phoneNumber: "",
        email: "",
      },
      teacherData: {},

      //表单验证规则
      rules: {
        phoneNumber: [
          {
            required: true,
            message: "请输入手机号码",
            trigger: "blur",
          },
          {
            validator: checkPhone,
            trigger: "blur",
          },
        ],
        email: [
          {
            required: true,
            message: "请输入邮箱",
            trigger: "blur",
          },
          {
            validator: checkEmail,
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    getTeacherData() {
      this.$api.GetTeacherData().then((res) => {
        this.teacherData = res.data.result;
      });
    },
    //编辑
    Edit() {
      this.isEdit = !this.isEdit;
    },
    //更新老师数据
    UpdateTeacherInfo() {
      this.$api.UpdateTeacherInfo(this.teacherInput).then((res) => {
        if (res.data.result) {
          this.$message.success("更新成功！");
        }else{
          this.$message.warning("更新失败！");
        }
      });
    },
  },
  created() {
    this.getTeacherData();
  },
};
</script>

<style lang="less" scoped>
.phone {
  width: 32%;
}
.teacherInfo {
  padding: 2% 5%;
}
.putTeacherInfo {
  margin-left: 5%;
}
.user_wrap {
  h3 {
    height: 60px;
    font-size: 30px;
    color: #438eb9;
    display: flex;
    align-items: center;
    padding-left: 25px;
    border-bottom: 2px solid #f3f3f3;
  }
  .user_info {
    padding: 2% 5%;
    display: flex;
    .user_avatar {
      flex: 2;
      text-align: center;
      img {
        height: 200px;
        width: 200px;
      }
      .updata_avatar {
        font-size: 16px;
        padding: 2%;
        margin-top: 2%;
        color: white;
        font-weight: 600;
        background-color: #7b9e31;
        &:hover {
          cursor: pointer;
        }
      }
    }
    .user_detail {
      flex: 8;
      padding: 0% 8%;
      .detail_header {
        padding-left: 2%;
        border-bottom: 1px solid #ccc;
        display: flex;
        justify-content: space-between;
        div {
          &:nth-child(1) {
            font-size: 16px;
            color: #478fca;
          }
          &:nth-child(2) {
            font-size: 15px;
            border-bottom: 2px solid #478fca;
            &:hover {
              cursor: pointer;
              color: #478fca;
            }
          }
          .anticon {
            padding: 0 8px;
          }
        }
      }
      .detail_body {
        .detail_item {
          width: 80%;
          margin: 1% auto;
          display: flex;
          .item_title {
            width: 20%;
            margin-right: 3%;
            font-size: 15px;
            color: #999;
            text-align: right;
          }
        }
      }
      .totalScore {
        color: red;
      }
      .groupCSS {
        color: red;
      }
    }
  }
}
</style>